﻿using Autofac;
using ZhouYu.DreamTour.BLL;
using ZhouYu.DreamTour.BLL.IService;
using ZhouYu.DreamTour.DAL;
using ZhouYu.DreamTour.DAL.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Autofac.Integration.Mvc;

namespace ZhouYu.DreamTour.Web.Dependcy
{
    public class AutofacConfig
    {
        public static void RegisterAutofac()  //自动注入类
        {
            // 创建一个容器
            var builder = new ContainerBuilder();
            Type basetype = typeof(IDenpendecy);
            var assemblys = AppDomain.CurrentDomain.GetAssemblies().ToList();
            builder.RegisterAssemblyTypes(assemblys.ToArray())
                .Where(t => basetype.IsAssignableFrom(t) && t != basetype)
                .AsImplementedInterfaces().InstancePerLifetimeScope();

            Type repository = typeof(IDenpendecyRepository);
            builder.RegisterAssemblyTypes(assemblys.ToArray())
                .Where(t => repository.IsAssignableFrom(t) && t != repository)
                .AsImplementedInterfaces().InstancePerLifetimeScope();
            builder.RegisterControllers(assemblys.ToArray());
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}